package com.proyecto.track.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import com.proyecto.track.bo.Period;
import com.proyecto.track.bo.Project;
import com.proyecto.track.bo.Task;
import com.proyecto.track.bo.TaskStatus;
import com.proyecto.track.bo.TaskType;
import com.proyecto.track.bo.UserPerson;
import com.proyecto.track.bo.UserTask;
import com.proyecto.track.dao.PeriodDAOImp;
import com.proyecto.track.dao.ProjectDAOImp;
import com.proyecto.track.dao.TaskDAOImp;
import com.proyecto.track.dao.TaskStatusDAOImp;
import com.proyecto.track.dao.TaskTypeDAOImp;

@Transactional(rollbackFor=Exception.class)
public class TaskService {
	private TaskDAOImp taskDAOImp;
	private TaskStatusDAOImp taskStatusDAOImp;
	private TaskTypeDAOImp taskTypeDAOImp;
	private ProjectDAOImp projectDAOImp;
	private PeriodDAOImp periodDAOImp;
	
	public ArrayList<TaskStatus> listTaskStatus(){
		return taskStatusDAOImp.searchTaskStatus();
	}
	
	public ArrayList<TaskType> listTaskType(){
		return taskTypeDAOImp.searchTaskType();
	}
	
	public Project createTask(Task task, Long idProject){
		Project project = projectDAOImp.searchProjects(idProject);
		project.getTasks().add(task);
		taskDAOImp.createTask(task);
		projectDAOImp.modifyProject(project);
		return project;
	}
	
	public void modifyTask(Task taskActualizar){
		Task task = taskDAOImp.searchTask(taskActualizar.getId());
		BeanUtils.copyProperties(taskActualizar, task);
		taskDAOImp.modifyTask(task);
	}
	
	public ArrayList<Task> searchTask(){
		return taskDAOImp.searchTask();
	}
	
	public ArrayList<Task> searchTask(Project project,UserPerson userPerson,Period period,TaskStatus taskStatus){
		return taskDAOImp.searchTask(project,userPerson,period,taskStatus);
	}
	
	public Task searchTask(Long id){
		return taskDAOImp.searchTask(id);
	}
	
	public void saveTypeTask(TaskType taskType ){
		taskTypeDAOImp.createTaskType(taskType);
	}
	
	public void saveStatusTask(TaskStatus taskStatus){
		taskStatusDAOImp.createTaskStatus(taskStatus);
	}
	
	public TaskDAOImp getTaskDAOImp() {
		return taskDAOImp;
	}
	public void setTaskDAOImp(TaskDAOImp taskDAOImp) {
		this.taskDAOImp = taskDAOImp;
	}
	
	public TaskStatusDAOImp getTaskStatusDAOImp() {
		return taskStatusDAOImp;
	}
	public void setTaskStatusDAOImp(TaskStatusDAOImp taskStatusDAOImp) {
		this.taskStatusDAOImp = taskStatusDAOImp;
	}
	
	public TaskTypeDAOImp getTaskTypeDAOImp() {
		return taskTypeDAOImp;
	}
	public void setTaskTypeDAOImp(TaskTypeDAOImp taskTypeDAOImp) {
		this.taskTypeDAOImp = taskTypeDAOImp;
	}

	public ProjectDAOImp getProjectDAOImp() {
		return projectDAOImp;
	}

	public void setProjectDAOImp(ProjectDAOImp projectDAOImp) {
		this.projectDAOImp = projectDAOImp;
	}

	public PeriodDAOImp getPeriodDAOImp() {
		return periodDAOImp;
	}

	public void setPeriodDAOImp(PeriodDAOImp periodDAOImp) {
		this.periodDAOImp = periodDAOImp;
	}

	public List<Task> listTasksToDo(Long accountId) {
		return taskDAOImp.listTasksToDo(accountId);
		// TODO Auto-generated method stub
		
	}
	public List<Task> listTasksByUser(Long accountId) {
		return taskDAOImp.listTasksByUser(accountId);
		// TODO Auto-generated method stub
		
	}

	public List<UserPerson> getUsers(long id) {
		
		Task task = this.searchTask(id);
		List<UserPerson> users = new ArrayList<UserPerson>();
		
		for (UserTask userTask :task.getUserTasks()) {
			if (userTask.isActive()){
			users.add(userTask.getUserPerson());
			}
		}
		return users;
	}

		public void modify(long id, List<UserPerson> newUserInTask) {
		
		Task task = taskDAOImp.searchTask(id);
		
		List<UserPerson> oldUsers = this.getUsers(id);
		List<UserPerson> changeStatusUsers = this.getUsers(id);
		
		changeStatusUsers.removeAll(newUserInTask);
		newUserInTask.removeAll(oldUsers);
				
		for (UserPerson userPerson : changeStatusUsers) {
			task.removeUser(userPerson.getUserTaskById(id));
		}

		
		for (UserPerson userPerson : newUserInTask) {
			task.addUsers(userPerson);
		}
	}

		public long searchProjectId(long id) {
			return taskDAOImp.searchProjectId(id);
		}
	
	
	
}
